//
//  TECacheTest.m
//  TintExample
//
//  Created by Kevin Wu on 9/3/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TECacheTest.h"


#define DATA_COUNT 10
#define DATA_CACHE_COUNT 10000
#define STRING_COUNT 1000
#define STRING_CACHE_COUNT 10000



@implementation TECacheTest

TTSynthesizeSingleton(TECacheTest);


#pragma mark - Accessors

@synthesize diskCache = _diskCache;

@synthesize addedData = _addedData;
@synthesize removedData = _removedData;
@synthesize addedString = _addedString;
@synthesize removedString = _removedString;


#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
        
        _diskCache = [TTDiskCache sharedObject];
        
        
        NSMutableArray *array = nil;
        
        array = [[NSMutableArray alloc] init];
        for ( int i=0; i<10000; ++i ) {
            NSString *name = [[NSString alloc] initWithFormat:@"data%05d", i+1];
            [array addObject:name];
            [name release];
        }
        _dataKeys = array;
        
        array = [[NSMutableArray alloc] init];
        for ( int i=0; i<10; ++i ) {
            NSString *name = [[NSString alloc] initWithFormat:@"img%02d.jpg", i+1];
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
            NSData *data = [[NSData alloc] initWithContentsOfFile:path];
            [array addObject:data];
            [data release];
            [name release];
        }
        _datas = array;
        
        
        
        array = [[NSMutableArray alloc] init];
        for ( int i=0; i<10000; ++i ) {
            NSString *name = [[NSString alloc] initWithFormat:@"string%05d", i+1];
            [array addObject:name];
            [name release];
        }
        _stringKeys = array;
        
        array = [[NSMutableArray alloc] init];
        NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
        NSUInteger lettersLength = [letters length];
        for ( int i=0; i<1000; ++i ) {
            NSUInteger length = (arc4random()%8 + 1) * 8;
            NSMutableString *string = [[NSMutableString alloc] init];
            for ( NSUInteger j=0; j<length; j++ ) {
                [string appendFormat:@"%c", [letters characterAtIndex:arc4random() % lettersLength]];
            }
            [array addObject:string];
            [string release];
        }
        _strings = array;
        
        
        
        _addedData = 0;
        _removedData = 0;
        _addedString = 0;
        _removedString = 0;
        
    }
    return self;
}

- (void)dealloc
{
    _diskCache = nil;
    
    TTRelease(_dataKeys);
    TTRelease(_datas);
    
    TTRelease(_stringKeys);
    TTRelease(_strings);
    
    _addedData = 0;
    _removedData = 0;
    _addedString = 0;
    _removedString = 0;
    
    [super dealloc];
}


#pragma mark - Public


- (void)addData:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            _addedData++;
            NSData *data = [_datas objectAtIndex:arc4random()%DATA_COUNT];
            NSString *key = [_dataKeys objectAtIndex:arc4random()%DATA_CACHE_COUNT];
            [_diskCache setData:data forKey:key withTimeoutInterval:arc4random()%1800+3600.0 name:@"test"];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
        }
        
        [NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)removeData:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            NSString *key = [_dataKeys objectAtIndex:arc4random()%DATA_CACHE_COUNT];
            if ( [_diskCache hasCacheForKey:key name:@"test"] ) {
                _removedData++;
                // NSLog(@"remove data: %@", key);
                [_diskCache removeCacheForKey:key name:@"test"];
            }
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
        }
        
        [NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}


- (void)addString:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            _addedString++;
            NSString *string = [_strings objectAtIndex:arc4random()%STRING_COUNT];
            NSString *key = [_stringKeys objectAtIndex:arc4random()%STRING_CACHE_COUNT];
            [_diskCache setString:string forKey:key withTimeoutInterval:arc4random()%1800+3600.0 name:@"test"];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
        }
        
    [NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}

- (void)removeString:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            NSString *key = [_stringKeys objectAtIndex:arc4random()%STRING_CACHE_COUNT];
            if ( [_diskCache hasCacheForKey:key name:@"test"] ) {
                _removedString++;
                // NSLog(@"remove string: %@", key);
                [_diskCache removeCacheForKey:key name:@"test"];
            }
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
        }
        
        [NSThread sleepForTimeInterval:[self randomTimeInterval]];
    }
    
    [pool release];
}



- (void)cleanCache:(id)object
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    int i = 0;
    
    while ( YES ) {
        if ( !_stop ) {
            [_diskCache cleanAllCache];
            
            i++;
            if ( i >= 100 ) {
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
                i = 0;
            }
        }
        
//        [NSThread sleepForTimeInterval:300.0];
    }
    
    [pool release];
}



#pragma mark - Override

- (void)startTest
{
    [super startTest];
    
    if ( !_launched ) {
        _launched = YES;
        [self performSelectorInBackground:@selector(addData:) withObject:nil];
        [self performSelectorInBackground:@selector(removeData:) withObject:nil];
        [self performSelectorInBackground:@selector(addString:) withObject:nil];
        [self performSelectorInBackground:@selector(removeString:) withObject:nil];
        [self performSelectorInBackground:@selector(cleanCache:) withObject:nil];
    }
}

@end
